/**
 * Created by cody on 2016/10/21.
 *
 * 饭否API
 *
 */

(function(window,document,$){

    var serverError = '服务器发生错误';
    var netError = '网络错误';
    function API(){

    }

    var tableLastUpdateDate;
    var tableTimerId;

    function getTable(storeId,success,error) {
        $.getJSON('/api/table/storeid/' + storeId,{},function(data){
            console.log(data)
            if(data){
                if(success) success(data);
            }else{
                if(error) error();
            }

        }).fail(function(){
            if(error) error();
        });
    }

    /**
     * 获取台桌状态变化列表
     * @param storeId
     * @param success
     * @param error
     */
    function getTableNotifyList(storeId,success,error) {
        var data = {
            lastUpdateDate:tableLastUpdateDate
        };
        $.getJSON('/waiterOrder/api/getTableNotifyList/'+storeId,data,function(data){
            console.log(data);
            if(data){
                if(success){
                    tableLastUpdateDate = data.lastUpdateDate;
                    console.log(tableLastUpdateDate);
                    success(data.tableList);
                }
            }else{
                if(error)
                    error();
            }
        }).fail(function(){
            if(error) error();
        });
    }

    /**
     * 定时获取台桌状态
     * @param storeId
     * @param success
     * @param error
     */
    function startTableStateTick(storeId,success,error) {
        getTableNotifyList(storeId,success,error);

        if(!tableTimerId){
            tableTimerId = setInterval(function () {
                console.log('startTableStateTick');
                getTableNotifyList(storeId,success,error)
            },10 * 1000);
        }
    }

    /**
     * 停止自动获取台桌状态
     */
    function stopTableStateTick() {
        if(tableTimerId){
            clearInterval(tableTimerId);
            tableTimerId = null;
        }
    }

    /**
     * 获取订单详情
     * @param parentOrderId
     */
    function getOrder(parentOrderId,success,error) {
        $.get('/waiterOrder/api/getParentOrderById/' + parentOrderId, function (data) {
            if(data && data.order){
                if(success)
                    success(data);
            }else{
                if(error)
                    error();
            }
        }).fail(function(){
            if(error)
                error();
        });
    }

    /**
     * 获取子订单详情接口
     * @param subOrderId
     * @param success
     * @param error
     */
    function getSubOrder(subOrderId,success,error) {
        $.getJSON('/waiterOrder/api/getSubOrderById/' + subOrderId, function (data) {
            if(data && data.order && data.menu){
                console.log(data);
                success(data);
            }else{
                error(serverError);
            }
        }).fail(function(){
            error(netError);
        });
    }
    /**
     * 取消订单
     */
    function refuseOrder(id,success,error){
        if(confirm("您确定拒绝此订单吗？")){
            $.getJSON('/waiterOrder/api/refuseOrder/'+id,'',function (data) {
                if(data.status === 'success'){
                   success(data);
                }else{
                    error('拒绝订单失败');
                }
            }).fail(function(){
                error('网络错误');
            });
        }
    }
    /**
     * 接受订单
     * @param id
     * @param $item
     */
    function acceptOrder(id,success,error) {
        $.getJSON('/waiterOrder/api/acceptOrder/' + id, '', function (data) {
            console.log(data);
            if (data.status === 'success') {
                success(data);
            } else {
                error('服务器发生错误');
            }
        }).fail(function(){
            error('网络错误');
        });
    }

    /**
     * 线下结账
     */
    API.prototype.offlinePay = function(orderId,userId,success,error){
        $.getJSON('/waiterOrder/api/offlinePay/'+userId+'/'+orderId,'',function(data){
            if(data.status === 'success'){
                success(data);
            }else{
                error(error);
            }
        }).fail(function(){
            error(error);
        });
    }


    /**
     * 台桌状态
     */
    API.prototype.getTable = getTable;
    API.prototype.getTableNotifyList = getTableNotifyList;
    API.prototype.startTableStateTick = startTableStateTick;
    API.prototype.stopTableStateTick = stopTableStateTick;
    /**
     * 获取子订单
     * @type {getSubOrder}
     */
    API.prototype.getSubOrder = getSubOrder;
    API.prototype.getOrder = getOrder;
    API.prototype.refuseOrder = refuseOrder;
    API.prototype.acceptOrder = acceptOrder;

    window.api = new API();

}(window,document,$));















